package cn.controller;

import cn.entity.Kind;
import cn.service.KindService;
import cn.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
//@Scope("prototype")
@RequestMapping("/kind")
public class KindController {//SSM
    @Autowired
    KindService kindService;
    @RequestMapping("admin/list")
    public String list(String name, Integer pageNo, Model model){
        PageBean pageBean=new PageBean();
        if(pageNo!=null){
            pageBean.setPageNo(pageNo);
        }
        //数量
        int totalCount=kindService.getCount(name);
        pageBean.setTotalCount(totalCount);
        List<Kind> list=kindService.find(name,pageBean);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("name", name);
        model.addAttribute("list", list);
        return "admin/kind";
    }

    @RequestMapping("jsonlist")
    @ResponseBody
    public List<Kind> jsonlist(){
        return kindService.find(null, null);
    }

    @RequestMapping("json")
    @ResponseBody
    public Kind json(Integer id){
        return kindService.selectByPrimaryKey(id);
    }
    @RequestMapping("admin/add")
    public String add(Kind kind){
        kind.setIsdel(0);
        int n=kindService.insert(kind);
        return "redirect:/kind/admin/list";
    }

    @RequestMapping("admin/update")
    public String update(Kind kind){
        int n=kindService.updateByPrimaryKey(kind);
        return "redirect:/kind/admin/list";
    }

    @RequestMapping("admin/delete")
    public String delete(Integer id){
       kindService.deleteByPrimaryKey(id);
        return "redirect:/kind/admin/list";
    }
}
